package com.mdp.im.core.subscriber;

import com.alibaba.fastjson.JSONObject;
import com.mdp.core.err.BizException;
import com.mdp.core.utils.LogUtils;
import com.mdp.im.core.plugin.api.SubscribePlugin;
import com.mdp.im.core.scbscriber.api.Subscriber;
import com.mdp.im.core.scbscriber.api.action.ScbscriberAction;
import com.mdp.im.core.subscriber.action.SubscriberActionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 队列订阅器实现类
 * 
 * @author chenyc
 * @date 2020/3/29
 */
@Service
public class MessageSubscriber implements Subscriber {
	
	 private static final Logger logger = LoggerFactory.getLogger(MessageSubscriber.class);

	@Autowired
	SubscriberActionFactory messageActionFactory;

	List<SubscribePlugin> plugins;

	@Override
	public void onSysMessage(String message) { 
		JSONObject json = JSONObject.parseObject(message);
		boolean isGo=beforeSub(json);
		if(isGo){
			String action=json.getString("action");
			String gloNo=json.getString("gloNo");
			if(StringUtils.hasText(gloNo)){
				MDC.put("gloNo",gloNo);
			}else {
				gloNo=LogUtils.genGloNo();
				json.put("gloNo",gloNo);
				MDC.put("gloNo",gloNo);
			}
			ScbscriberAction subscriberAction = messageActionFactory.getMessageAction(action);
			if(subscriberAction == null) {
				throw new BizException("Invalid action, " + action);
			}

			subscriberAction.doAction(json);
		}
		afterSub(isGo,json);

	    
	}
	@Override
	public void onPrichatMessage(String message) {
        JSONObject json = JSONObject.parseObject(message);
		boolean isGo=beforeSub(json);
		if(isGo){
			String action=json.getString("action");
			String gloNo=json.getString("gloNo");
			if(StringUtils.hasText(gloNo)){
				MDC.put("gloNo",gloNo);
			}else {
				gloNo=LogUtils.genGloNo();
				json.put("gloNo",gloNo);
				MDC.put("gloNo",gloNo);
			}
			ScbscriberAction subscriberAction = messageActionFactory.getMessageAction(action);
			if(subscriberAction == null) {
				throw new BizException("Invalid action, " + action);
			}
			subscriberAction.doAction(json);
		}
		afterSub(isGo,json);

		
	}
	@Override
	public void onGroupMessage(String groupId, String message) {
        JSONObject json = JSONObject.parseObject(message);
		boolean isGo=beforeSub(json);
		if(isGo){
			String action=json.getString("action");
			String gloNo=json.getString("gloNo");
			if(StringUtils.hasText(gloNo)){
				MDC.put("gloNo",gloNo);
			}else {
				gloNo=LogUtils.genGloNo();
				json.put("gloNo",gloNo);
				MDC.put("gloNo",gloNo);
			}
			ScbscriberAction subscriberAction = messageActionFactory.getMessageAction(action);
			if(subscriberAction == null) {
				throw new BizException("Invalid action, " + action);
			}

			subscriberAction.doAction(json);
		}
		afterSub(isGo,json);
		
	}

	boolean beforeSub(JSONObject message){
		if(this.plugins!=null && this.plugins.size()>0){
			boolean isGo=true;
			for (SubscribePlugin plugin : this.plugins) {
				if(isGo){
					isGo=plugin.beforeSubscribe(message);
				} else if(isGo==false){
					plugin.beforeSubscribe(message);
				}
			}
			return isGo;
		}
		return true;
	}

	void afterSub(boolean isGo,JSONObject message){
		if(this.plugins!=null && this.plugins.size()>0){
			for (SubscribePlugin plugin : this.plugins) {
				plugin.afterSubscribe(isGo,message);
			}
		}
	}

}
